class CreatePanel extends BasePanel {

    protected duijuConfig = {
        2: {
            1: {count: 6, cost: 4, type: RoundType.duiju},
            2: {count: 12, cost: 4, type: RoundType.duiju},
            3: {count: 26, cost: 6, type: RoundType.duiju},
        },
        3: {
            1: {count: 5, cost: 4, type: RoundType.duiju},
            2: {count: 10, cost: 4, type: RoundType.duiju},
            3: {count: 20, cost: 6, type: RoundType.duiju},
        },
        4: {
            1: {count: 1, cost: 4, type: RoundType.quan},
            2: {count: 2, cost: 4, type: RoundType.quan},
            3: {count: 4, cost: 6, type: RoundType.quan},
        }
    };

    protected scroller: eui.Scroller;
    protected group: eui.Group;

    protected btn_type_1: eui.RadioButton;
    protected btn_type_2: eui.RadioButton;

    protected btn_round_1: eui.RadioButton;
    protected btn_round_2: eui.RadioButton;
    protected btn_round_3: eui.RadioButton;

    protected btn_seat_2: eui.RadioButton;
    protected btn_seat_3: eui.RadioButton;
    protected btn_seat_4: eui.RadioButton;

    protected costGroup: eui.Group;
    protected btn_cost_1: eui.RadioButton;//房主支付
    protected btn_cost_2: eui.RadioButton;//AA制

    protected ruleGroup: eui.Group;

    protected btn_start: eui.Button;

    public constructor() {
        super();

        this.skinName = "CreatePanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        if (GameConfig.isPureness) {
            this.btn_cost_2.label = '均摊支付';
        }

        for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
            let btn = this.ruleGroup.getElementAt(i);
            if (btn) {
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
            }
        }

        this.localRead();
        this.update();

        this.btn_round_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_round_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_round_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_cost_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_cost_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        this.btn_seat_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_seat_3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_seat_4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
            let btn = this.ruleGroup.getElementAt(i);
            if (btn) {
                btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
            }
        }

        this.btn_start.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent): void {
        switch (e.currentTarget) {
            case this.btn_start:
                this.sendCreate();
                break;
            default:
                break;
        }

        this.update();
        this.localSave();
    }
    /**发送创建房间 */
    protected sendCreate() {
        let config = this.duijuConfig[this.getSeat()];
        if (config) {
            let cmd: number = game.isDaikai ? 30 : 2;
            gameManager.socketManager.send(cmd, {
                type: GameConfig.gameType,
                pass: "0",
                round: config[this.getRound()].count,
                round_type: config[this.getRound()].type,
                ren: this.getSeat(),
                rules: this.getRule()
            });

            game.roomRoundType = config[this.getRound()].type;
            game.roomRound = config[this.getRound()].count;
            //圈需要转换
            game.roomRound = game.roomRoundType == RoundType.quan ? game.roomRound * 4 : game.roomRound;
        }
    }

    getRound() {
        if (this.btn_round_1.selected) {
            return 1;
        }
        else if (this.btn_round_2.selected) {
            return 2;
        }
        else if (this.btn_round_3.selected) {
            return 3;
        }
    }

    getSeat(): number {
        if (this.btn_seat_2.selected) {
            return 2;
        }
        else if (this.btn_seat_3.selected) {
            return 3;
        }
        else if (this.btn_seat_4.selected) {
            return 4;
        }
    }

    getCost(): number {
        if (this.btn_cost_1.selected) {
            return 1;
        }
        else if (this.btn_cost_2.selected) {
            return 2;
        }
    }

    getRule() {
        let rule: any[] = [];

        rule.push(RuleType.ting);

        if (!game.isDaikai) {
            //支付
            switch (this.getCost()) {
                case 1:
                    rule.push(RuleType.fangzhuzhifu);
                    break;
                case 2:
                    rule.push(RuleType.aazhi);
                    break;
            }
        }

        let box: eui.CheckBox;
        for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
            box = this["btn_rule_" + i];
            if (box && box.selected) {
                switch (i) {
                    case 0:
                        rule.push(RuleType.baoyijia);
                        break;
                    case 1:
                        rule.push(RuleType.dianpaobaosanjia);
                        break;
                    case 2:
                        rule.push(RuleType.chikaimen);
                        break;
                    case 3:
                        rule.push(RuleType.menhu);
                        break;
                    case 4:
                        rule.push(RuleType.baotingjiabei);
                        break;
                    case 5:
                        rule.push(RuleType.zhangmao);
                        break;
                    case 6:
                        rule.push(RuleType.xiganghu);
                        break;
                    case 7:
                        rule.push(RuleType.qingyise);
                        break;
                    case 8:
                        rule.push(RuleType.siguiyi);
                        break;
                }
            }
        }

        return rule;
    }

    update() {
        let config = this.duijuConfig[this.getSeat()];
        if (config) {
            this.btn_round_1.label = config[1].count + (config[1].type == RoundType.quan ? "圈" : "局") + "(房卡x" + (this.getCost() == 2 && !game.isDaikai ? Math.ceil(config[1].cost / this.getSeat()) + '/人' : config[1].cost) + ")";
            this.btn_round_2.label = config[2].count + (config[2].type == RoundType.quan ? "圈" : "局") + "(房卡x" + (this.getCost() == 2 && !game.isDaikai ? Math.ceil(config[2].cost / this.getSeat()) + '/人' : config[2].cost) + ")";
            this.btn_round_3.label = config[3].count + (config[3].type == RoundType.quan ? "圈" : "局") + "(房卡x" + (this.getCost() == 2 && !game.isDaikai ? Math.ceil(config[3].cost / this.getSeat()) + '/人' : config[3].cost) + ")";

        }
    }

    show() {
        super.show();
        this.skinState = "normal";
        this.localRead();
        this.update();

        if (game.isDaikai) {
            if (this.group.contains(this.costGroup)) {
                this.group.removeChild(this.costGroup);
            }
        }
        else {
            if (!this.group.contains(this.costGroup)) {
                this.group.addChildAt(this.costGroup, 1);
            }
        }
    }

    localSave() {
        GameStorage.setData(GameStorage.round, this.getRound());
        GameStorage.setData(GameStorage.cost, this.getCost());
        GameStorage.setData(GameStorage.seat, this.getSeat());

        let rules: any[] = [];
        let box: eui.CheckBox;
        for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
            box = this["btn_rule_" + i];
            if (box) {
                rules.push(box.selected);
            }
        }

        GameStorage.setData(GameStorage.rule, rules.join(`#`));
    }

    localRead() {
        //回合
        if (GameStorage.getData(GameStorage.round)) {
            this[`btn_round_` + GameStorage.getData(GameStorage.round)].selected = true;
        }
        else {
            this.btn_round_2.selected = true;
        }

        //支付
        if (GameStorage.getData(GameStorage.cost)) {
            this[`btn_cost_` + GameStorage.getData(GameStorage.cost)].selected = true;
        }
        else {
            this.btn_cost_1.selected = true;
        }

        //人数
        if (GameStorage.getData(GameStorage.seat)) {
            this[`btn_seat_` + GameStorage.getData(GameStorage.seat)].selected = true;
        }
        else {
            this.btn_seat_4.selected = true;
        }

        //规则
        if (GameStorage.getData(GameStorage.rule)) {
            let rules: string[] = GameStorage.getData(GameStorage.rule).split("#");
            let box: eui.CheckBox;
            for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
                box = this["btn_rule_" + i];
                if (box) {
                    box.selected = rules[i] == `true`;
                }
            }
        }
        else {
            let box: eui.CheckBox;
            for (let i: number = 0; i < this.ruleGroup.numElements; i++) {
                box = this["btn_rule_" + i];
                if (box) {
                    switch (i) {
                        case 1:
                        case 3:
                        case 4:
                            box.selected = true;
                            break;
                        default:
                            box.selected = false;
                            break;
                    }
                }
            }
        }
    }
}